package com.xcms.test;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LastAccessTimeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Cookie[] cookies = request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for (Cookie cookie:cookies) {
                if(cookie.getName().equals("lasttime"))
                    out.println("你上次的访问时间是："+cookie.getValue());
            }
        }
        //如果第一次访问或者后面访问都有更新时间
        Cookie cookie=new Cookie("lasttime",System.currentTimeMillis()+"");
        cookie.setMaxAge(60*60);
        cookie.setPath("/xcms");

        response.addCookie(cookie);
    }
}
